package com.kfm.servlet.day03;

import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(value= "/captcha", loadOnStartup = 1)
public class CaptchaServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        // 动态注册Servlet
        ServletRegistration.Dynamic captcha = getServletContext().addServlet("captcha", com.wf.captcha.servlet.CaptchaServlet.class);
        captcha.addMapping("/captcha1");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 4位验证码
//        CaptchaUtil.out(req, resp);
        // 使用gif验证码
        GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
        CaptchaUtil.out(gifCaptcha, req, resp);
    }
}
